package com.sugar.subscribe.course.module.business.controller.admin.config;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.*;

import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.CommonResult;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;
import static com.sugar.subscribe.course.framework.common.pojo.CommonResult.success;
import static com.sugar.subscribe.course.module.business.enums.config.BizConfigEnum.HOME_BANNER;

import com.sugar.subscribe.course.module.business.controller.admin.config.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.config.BizConfigDO;
import com.sugar.subscribe.course.module.business.service.config.BizConfigService;

@Tag(name = "管理后台 - 参数配置")
@RestController
@RequestMapping("/business/config")
@Validated
public class BizConfigController {

    @Resource
    private BizConfigService bizConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建参数配置")
    @PreAuthorize("@ss.hasPermission('business:config:create')")
    public CommonResult<Long> createConfig(@Valid @RequestBody BizConfigSaveReqVO createReqVO) {
        return success(bizConfigService.createConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新参数配置")
    @PreAuthorize("@ss.hasPermission('business:config:update')")
    public CommonResult<Boolean> updateConfig(@Valid @RequestBody BizConfigSaveReqVO updateReqVO) {
        if (updateReqVO.getConfigKey().equals(HOME_BANNER.getValue())){
            Object value = updateReqVO.getValue();
            if (value != null){
                updateReqVO.setValue(String.join(",", JSONUtil.toList(JSONUtil.toJsonStr(value), String.class)));
            }
        }

        bizConfigService.updateConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除参数配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:config:delete')")
    public CommonResult<Boolean> deleteConfig(@RequestParam("id") Long id) {
        bizConfigService.deleteConfig(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除参数配置")
                @PreAuthorize("@ss.hasPermission('business:config:delete')")
    public CommonResult<Boolean> deleteConfigList(@RequestParam("ids") List<Long> ids) {
        bizConfigService.deleteConfigListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得参数配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:config:query')")
    public CommonResult<BizConfigRespVO> getConfig(@RequestParam("id") Long id) {
        BizConfigDO config = bizConfigService.getConfig(id);
        BizConfigRespVO respVO = BeanUtils.toBean(config, BizConfigRespVO.class);
        if (config.getConfigKey().equals(HOME_BANNER.getValue())){
            Object value = respVO.getValue();
            if (value != null){
                respVO.setValue(String.valueOf(value).split(","));
            }
        }
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得参数配置分页")
    @PreAuthorize("@ss.hasPermission('business:config:query')")
    public CommonResult<PageResult<BizConfigRespVO>> getConfigPage(@Valid BizConfigPageReqVO pageReqVO) {
        PageResult<BizConfigDO> pageResult = bizConfigService.getConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BizConfigRespVO.class));
    }
}